package com.example.api;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ComputeCodeColNum {
    //思路
        //C:\Users\kelvin\Desktop\java2\shixun\day12\代码\waimai\apiServer
            //所有xx.java文件先找到
                  //读一行计数器+1
    static int count=0;
    public static void main(String[] args) {
        String startPath = "C:\\Users\\86157\\Desktop\\waimai\\apiServer";
        listFile(new File(startPath));
        System.out.println(count);
    }
    private static void listFile(File parentFile){
        if(parentFile.isDirectory()){  //判断是否文件夹
            File [] childrenFile =  parentFile.listFiles(); //罗列里面的所有文件，包括文件夹
            for(File childFile :childrenFile){
                listFile(childFile);
            }
        }else{
            String fileName = parentFile.getName();
            if(fileName.endsWith(".java")){
                System.out.println(parentFile.getName());
                //读取该文件
                   //读一行count+1
                int xxCount = readLineCount(parentFile.getAbsolutePath());

                System.out.println(fileName + "的行数" + xxCount);
                count+=xxCount;
            }
            if(fileName.endsWith(".xml")){
                System.out.println(parentFile.getName());
                //读取该文件
                //读一行count+1
                int xxCount = readLineCount(parentFile.getAbsolutePath());

                System.out.println(fileName + "的行数" + xxCount);
                count+=xxCount;
            }
        }
    }

    private static int readLineCount(String path){
        int count = 0;
        BufferedReader bufferedReader=null;
        try {
            bufferedReader = new BufferedReader(new FileReader(path));

            String content = null;

            while((content= bufferedReader.readLine())!=null){

                if("".equals(content.trim())){
                    continue;
                }
                count++;

            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
        return count;
    }
}
